package cn.yitun.chat.yitunservermanagecore.general.manage.common.controller;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitungeneral.annotation.parameter.ParamDefine;
import cn.yitun.chat.yitungeneral.annotation.parameter.RequestParameter;
import cn.yitun.chat.yitunservercommonlib.annotation.Log;
import cn.yitun.chat.yitunservercommonlib.basebean.web.JwtUserDto;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.BaseController;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.common.DictDataDto;
import cn.yitun.chat.yitunservermanagecore.general.bridge.auth.annotation.PermissionMapping;
import cn.yitun.chat.yitunservermanagecore.general.manage.common.service.DictDataService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 字典数据表
 * 
 * @author admin
 * @email chen@163.com
 * @date 2019-02-15 09:16:13
 */
 
@RestController
@RequestMapping("/manage/common")
public class DictDataController extends BaseController {
	@Autowired
	private DictDataService dictDataService;

	/**
	 * 根据字典类型查询字典数据信息
	 *
	 * @param dictType 字典类型
	 * @return 参数键值
	 */
	@RequestParameter
	@PermissionMapping(name = "类型下字典数据", perms = "common:dictType:dictType")
	@GetMapping("/dictData/getType/{dictType}")
	public ResultMessage getType(@PathVariable( "dictType") String dictType) {
		ResultMessage rm = new ResultMessage();
		rm.bodyPut("list", dictDataService.findListByType(dictType));
		return rm;
	}

	@RequestParameter
	@PermissionMapping(name = "字典数据单条", perms = "common:dictType:dictType")
	@GetMapping("/dictData/detail")
	public ResultMessage detail(@ParamDefine("id") Long id){
		ResultMessage rm = new ResultMessage();
		rm.bodyPut("info", dictDataService.getVO(id));
		return rm;
	}

	@RequestParameter
	@PermissionMapping(name = "字典数据分页", perms = "common:dictType:dictType")
	@PostMapping("/dictData/page")
	public ResultMessage list(JwtUserDto jwtUserDto, @JsonDefine DictDataDto dictDataDto){
		//查询列表数据
		checkRootPage(dictDataDto, jwtUserDto);
		PageHelper.startPage(dictDataDto.getCurrentPageNo() , dictDataDto.getPageSize());
		ResultMessage rm = new ResultMessage();
		rm.bodyPut("page", new PageInfo<>(dictDataService.listVO(dictDataDto)));
		return rm;
	}

	/**
	 * 保存
	 */
	@Log("字典详情新增")
	@RequestParameter
	@PermissionMapping(name = "字典详情新增", perms = "common:dictType:dictType")
	@PostMapping("/dictData/save")
	public ResultMessage save(JwtUserDto jwtUserDto, @JsonDefine  DictDataDto dictDataDto){
		setUserMsgSave(dictDataDto, jwtUserDto);
		if (dictDataService.save(dictDataDto) == 0)
			throw new YitunException(ExceptionEnum.ADD_FAIL);
		return new ResultMessage();
	}
	/**
	 * 修改
	 */
	@Log("字典详情编辑")
	@RequestParameter
	@PermissionMapping(name = "字典详情编辑", perms = "common:dictType:dictType")
	@RequestMapping("/dictData/update")
	public ResultMessage update(JwtUserDto jwtUserDto,@JsonDefine  DictDataDto dictDataDto){
		if (dictDataDto.getId() == null )
			throw new YitunException(ExceptionEnum.REQUEST_PARAM_NULL);

		setUserMsgUpdate(dictDataDto, jwtUserDto);
		if (dictDataService.update(dictDataDto) == 0)
			throw new YitunException(ExceptionEnum.EDIT_FAIL);
		return new ResultMessage();
	}

	/**
	 * 删除
	 */
	@Log("字典详情删除")
	@RequestParameter
	@PermissionMapping(name = "字典详情删除", perms = "common:dictType:dictType")
	@DeleteMapping( "/dictData/remove/{id}")
	public ResultMessage remove(@PathVariable("id") Long id){
		if (dictDataService.remove(id) == 0)
			throw new YitunException(ExceptionEnum.DELETE_FAILED);
		return new ResultMessage();
	}

	//---------------------------旧接口分隔线--------------------------------------



	/**
	 * 批量删除
	 */
	@Log("字典详情批量删除")
	@RequestParameter
	@PermissionMapping(name = "字典详情批量删除", perms = "common:dictType:dictType")
	@DeleteMapping( "/dictData/batchRemove/{ids}")
	public ResultMessage remove(@PathVariable("ids") Long[] ids){
		if (dictDataService.batchRemove(ids) == 0)
			throw new YitunException(ExceptionEnum.DELETE_FAILED);
		return new ResultMessage();
	}

}
